package com.ihd.ihardware.view.tzc.health.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.common.utils.DataUtils;
import com.ihd.ihardware.databinding.ActivityMeasureBinding;
import com.ihd.ihardware.db.MDataBase;
import com.ihd.ihardware.db.Weight;
import com.ihd.ihardware.pojo.ReportRes;
import com.ihd.ihardware.view.tzc.health.viewmodel.MeasureViewModel;

import cn.wowjoy.commonlibrary.app.Constans;
import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DateUtils;
import cn.wowjoy.commonlibrary.utils.HexUtils;
import cn.wowjoy.commonlibrary.utils.HttpUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;

public class MeasureActivity extends BaseActivity<ActivityMeasureBinding, MeasureViewModel> {
    private TextToSpeech textToSpeech;
    private Animation rotate;
    private Animation rotate1;
    private Animation bg;
    private String familyId;
    private String originalData;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();

        familyId = getIntent().getStringExtra("familyId");
        originalData = getIntent().getStringExtra("originalData");

        String wei = DataUtils.getWeight(HexUtils.hexStringToBytes(originalData));
        binding.weightTV.setText(wei.split("\\.")[0]);
        binding.weightPointTV.setText("." + wei.split("\\.")[1]);

        if (SPUtils.getBoolean(AppConstans.VOICE, true)) {
            textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    textToSpeech.setPitch(0.0f);
                    textToSpeech.speak("您的体重唯" + wei + "公斤", TextToSpeech.QUEUE_FLUSH, null, null);
                }
            });
        }
        rotate = AnimationUtils.loadAnimation(this, R.anim.measure_rotate_anim_go);
        rotate1 = AnimationUtils.loadAnimation(this, R.anim.measure_rotate_anim_back);
        bg = AnimationUtils.loadAnimation(this, R.anim.measure_rotate_anim_all);

        LinearInterpolator lin = new LinearInterpolator();
        bg.setInterpolator(lin);

        DecelerateInterpolator din = new DecelerateInterpolator();
        rotate.setInterpolator(din);
        rotate1.setInterpolator(din);

        binding.btBG.startAnimation(bg);

        rotate.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animation.cancel();
                binding.rotateIV.startAnimation(rotate1);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        rotate1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animation.cancel();
                binding.rotateIV.startAnimation(rotate);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        binding.rotateIV.startAnimation(rotate);


        setRx(AppConstans.REPORT, new BaseConsumer<ReportRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(ReportRes mLiveData) {
                Intent i = new Intent(MeasureActivity.this, ReportActivity.class);
                i.putExtra("report", mLiveData.getData());
                startActivity(i);
                finish();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(MeasureActivity.this, error.getMessage());
            }
        });

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String token = SPUtils.getString(Constans.GATEWAY_TOKEN, "");
                JsonObject mParam = new JsonParser().parse(token).getAsJsonObject();
                Weight user = new Weight(mParam.get("userId").getAsString(), familyId, originalData, DateUtils.getCurrFullTime());
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        MDataBase.getInstance(getApplicationContext()).getWeightDao().insert(user);
                    }
                }).start();

                if (HttpUtils.getInstance().isNetworkConnected())
                    viewModel.weighing(familyId, originalData);
            }
        }, 1000);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_measure;
    }

    @Override
    protected Class getViewModelClass() {
        return MeasureViewModel.class;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding.btBG.clearAnimation();
        binding.rotateIV.clearAnimation();
//        textToSpeech.stop();
//        textToSpeech.shutdown();
    }

    @Override
    public void onBackPressed() {
    }
}
